<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="结算年月" prop="yearmon">
        <el-input v-model="dataForm.yearmon" placeholder="结算年月" />
      </el-form-item>
      <el-form-item label="变更类型" prop="changetype">
        <el-input v-model="dataForm.changetype" placeholder="变更类型" />
      </el-form-item>
      <el-form-item label="操作时间" prop="optime">
        <el-input v-model="dataForm.optime" placeholder="操作时间" />
      </el-form-item>
      <el-form-item label="employee.id" prop="employeeId">
        <el-input v-model="dataForm.employeeId" placeholder="employee.id" />
      </el-form-item>
      <el-form-item label="在用/停用  1：在用；0：停用" prop="isUse">
        <el-input
          v-model="dataForm.isUse"
          placeholder="在用/停用  1：在用；0：停用"
        />
      </el-form-item>
      <el-form-item label="考勤系统用户ID" prop="userinfoId">
        <el-input v-model="dataForm.userinfoId" placeholder="考勤系统用户ID" />
      </el-form-item>
      <el-form-item label="部门编号" prop="sysOrganizationId">
        <el-input v-model="dataForm.sysOrganizationId" placeholder="部门编号" />
      </el-form-item>
      <el-form-item label="职务编号" prop="dutyId">
        <el-input v-model="dataForm.dutyId" placeholder="职务编号" />
      </el-form-item>
      <el-form-item label="夜间值班分组ID on_call.ID" prop="onCallId">
        <el-input
          v-model="dataForm.onCallId"
          placeholder="夜间值班分组ID on_call.ID"
        />
      </el-form-item>
      <el-form-item label="性别 0：女；1：男" prop="sex">
        <el-input v-model="dataForm.sex" placeholder="性别 0：女；1：男" />
      </el-form-item>
      <el-form-item label="类别 0：聘用；1：在编；" prop="type">
        <el-input
          v-model="dataForm.type"
          placeholder="类别 0：聘用；1：在编；"
        />
      </el-form-item>
      <el-form-item label="性别 0：离职；1：在职；2：借调" prop="state">
        <el-input
          v-model="dataForm.state"
          placeholder="性别 0：离职；1：在职；2：借调"
        />
      </el-form-item>
      <el-form-item label="员工姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="员工姓名" />
      </el-form-item>
      <el-form-item label="工号" prop="code">
        <el-input v-model="dataForm.code" placeholder="工号" />
      </el-form-item>
      <el-form-item label="是否职工遗属" prop="isEmployeeSurvivors">
        <el-input
          v-model="dataForm.isEmployeeSurvivors"
          placeholder="是否职工遗属"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        yearmon: "",
        changetype: "",
        optime: "",
        employeeId: "",
        isUse: "",
        userinfoId: "",
        sysOrganizationId: "",
        dutyId: "",
        onCallId: "",
        sex: "",
        type: "",
        state: "",
        name: "",
        code: "",
        isEmployeeSurvivors: ""
      },
      dataRule: {
        yearmon: [
          { required: true, message: "结算年月不能为空", trigger: "blur" }
        ],
        changetype: [
          { required: true, message: "变更类型不能为空", trigger: "blur" }
        ],
        optime: [
          { required: true, message: "操作时间不能为空", trigger: "blur" }
        ],
        employeeId: [
          { required: true, message: "employee.id不能为空", trigger: "blur" }
        ],
        isUse: [
          {
            required: true,
            message: "在用/停用  1：在用；0：停用不能为空",
            trigger: "blur"
          }
        ],
        userinfoId: [
          {
            required: true,
            message: "考勤系统用户ID不能为空",
            trigger: "blur"
          }
        ],
        sysOrganizationId: [
          { required: true, message: "部门编号不能为空", trigger: "blur" }
        ],
        dutyId: [
          { required: true, message: "职务编号不能为空", trigger: "blur" }
        ],
        onCallId: [
          {
            required: true,
            message: "夜间值班分组ID on_call.ID不能为空",
            trigger: "blur"
          }
        ],
        sex: [
          {
            required: true,
            message: "性别 0：女；1：男不能为空",
            trigger: "blur"
          }
        ],
        type: [
          {
            required: true,
            message: "类别 0：聘用；1：在编；不能为空",
            trigger: "blur"
          }
        ],
        state: [
          {
            required: true,
            message: "性别 0：离职；1：在职；2：借调不能为空",
            trigger: "blur"
          }
        ],
        name: [
          { required: true, message: "员工姓名不能为空", trigger: "blur" }
        ],
        code: [{ required: true, message: "工号不能为空", trigger: "blur" }],
        isEmployeeSurvivors: [
          { required: true, message: "是否职工遗属不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/employeeChange/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.yearmon = data.employeeChange.yearmon;
              this.dataForm.changetype = data.employeeChange.changetype;
              this.dataForm.optime = data.employeeChange.optime;
              this.dataForm.employeeId = data.employeeChange.employeeId;
              this.dataForm.isUse = data.employeeChange.isUse;
              this.dataForm.userinfoId = data.employeeChange.userinfoId;
              this.dataForm.sysOrganizationId =
                data.employeeChange.sysOrganizationId;
              this.dataForm.dutyId = data.employeeChange.dutyId;
              this.dataForm.onCallId = data.employeeChange.onCallId;
              this.dataForm.sex = data.employeeChange.sex;
              this.dataForm.type = data.employeeChange.type;
              this.dataForm.state = data.employeeChange.state;
              this.dataForm.name = data.employeeChange.name;
              this.dataForm.code = data.employeeChange.code;
              this.dataForm.isEmployeeSurvivors =
                data.employeeChange.isEmployeeSurvivors;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/employeeChange/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              yearmon: this.dataForm.yearmon,
              changetype: this.dataForm.changetype,
              optime: this.dataForm.optime,
              employeeId: this.dataForm.employeeId,
              isUse: this.dataForm.isUse,
              userinfoId: this.dataForm.userinfoId,
              sysOrganizationId: this.dataForm.sysOrganizationId,
              dutyId: this.dataForm.dutyId,
              onCallId: this.dataForm.onCallId,
              sex: this.dataForm.sex,
              type: this.dataForm.type,
              state: this.dataForm.state,
              name: this.dataForm.name,
              code: this.dataForm.code,
              isEmployeeSurvivors: this.dataForm.isEmployeeSurvivors
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
